package com.huayue.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.PurchaseOrder;
import com.huayue.entity.PurchaseOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.EmployeeService;
import com.huayue.service.ProductService;
import com.huayue.service.PurchaseOrderService;
import com.huayue.service.SupplierService;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 进货单
 * 
 */
@Controller
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {
    @Autowired
    PurchaseOrderService svc;

    @Autowired
    EmployeeService empSvc;

    @Autowired
    ProductService proSvc;

    @Autowired
    SupplierService supSvc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(PurchaseOrder purchaseorder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(purchaseorder.getOrdernumber())) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.JHDORDER, "JHD"));
        }
        view.addStaticAttribute("product", proSvc.findProductByReal_Number());
        view.addStaticAttribute("supplier", supSvc.findAll());
        return new ModelAndView(view);
    }

    /**
     * 清除订单号
     * 
     * @param purchaseorder
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(PurchaseOrder purchaseorder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.JHDORDER, purchaseorder.getOrdernumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-请购单")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 格式化json数据
        JSONObject jsonData = JSONObject.parseObject(json);
        // 创建订单信息
        PurchaseOrder order = new PurchaseOrder();
        order.setEmployee_id_apply(user.getId());
        order.setOrdernumber(String.valueOf(jsonData.get("orderNumber")));
        order.setSupplier_id(Integer.valueOf(String.valueOf(jsonData.get("supplierId"))));
        order.setOtherprice(BigDecimal.valueOf(Double.parseDouble(jsonData.get("otherPrice").toString())));
        order.setRemark(jsonData.get("remark").toString());
        JSONArray jsonArray = jsonData.getJSONArray("table");
        // 获取订单详情
        List<PurchaseOrderDetails> details = JSONArray.parseArray(jsonArray.toString(), PurchaseOrderDetails.class);
        // 将订单信息和订单详情传至service进行处理
        boolean result = svc.addDetails(order, details);
        // 将临时订单号销毁
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.JHDORDER, order.getOrdernumber());
        // 返回数据给页面
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "审核-请购单")
    @RequestMapping("/check")
    public ModelAndView check(PurchaseOrder purchaseorder, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        purchaseorder.setEmployee_id_retify(user.getId());
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        int s = svc.check(purchaseorder);
        if (s > 0) {
            view.addStaticAttribute("msg", "操作成功！");
            view.addStaticAttribute("icon", 1);
        } else {
            view.addStaticAttribute("msg", "操作失败！");
            view.addStaticAttribute("icon", 2);
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "修改-请购单")
    @RequestMapping("/update")
    public ModelAndView update(PurchaseOrder purchaseorder) {
        return bc.update(svc, purchaseorder);
    }

    @LogAnno(operateType = "删除-请购单")
    @RequestMapping("/delete")
    public ModelAndView delete(PurchaseOrder purchaseorder) {
        return bc.delete(svc, purchaseorder);
    }

    // 这个是查看详情要用的
    @RequestMapping("/findDetails")
    public ModelAndView findDetails(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        PurchaseOrder order = new PurchaseOrder();
        order.setId(Integer.valueOf(id));
        // 先找到一个
        view.addStaticAttribute("order", svc.findOne(order));
        // 然后传数据
        view.addStaticAttribute("data", svc.findDetails(id));
        return new ModelAndView(view);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(PurchaseOrder purchaseorder) {
        return bc.findOne(svc, purchaseorder);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, PurchaseOrder purchaseorder) {
        return bc.findPage(svc, page, purchaseorder, purchaseorder);
    }
}
